

//方法必须在类里
public class Main {

    public static int sum(int a, int b) {
        return a + b;
    }
    public static int sum(int a, int b,int c) {//参数数量不一样
        return a + b;
    }
    public static int sum(double d,int a, int b) {
        return a + b;
    }
    public static int sum(int a, double d,int b) {//参数顺序不一样
        return a + b;
    }
    public static double sum(double a, double b) {//参数类型不一样
        return a + b;
    }
    //函数的重载
    public static void main3(String[] args) {
        int a = 10, b = 20;
        System.out.println(sum(a,b));
        double c =1.31,d = 12.21;
        System.out.println(sum(c,d));
    }

    public  static  int[] Swap(int[]arr){
        int tmp = arr[0];
        arr[0] = arr[1];
        arr[1] = tmp;
        return arr;
    }
    //交换两个变量的值
    public static void main2(String[] args) {
        int a = 10;
        int b = 20;
        System.out.println("a="+a+",b="+b);
        int tmp = a;
        a = b;
        b = tmp;
        System.out.println("a="+a+",b="+b);
        //调用函数 -- Java没有指针 - 通过new或者数组(在堆上存放)才能交换
        int[] arr = {a,b};
        Swap(arr);
        System.out.println("a="+arr[0]+",b="+arr[1]);
    }

    public static int fac(int n){
        if(n == 0){
            return 1;
        }
        else {
            return n*fac(n-1);
        }
    }
    //方法名采用小驼峰命名
    public static int add(int a, int b) {
        System.out.println("a="+1+",b="+b);
        System.out.println("Result="+(a+b));
        return a + b;
    }
    /*public static void add(int a,int b) {//无返回值必须用void
        System.out.println(a+b);
    }*/
    public static void func(int n){
        n = 10;
        System.out.println("改变了的形参n="+n);
    }
    public static void main1(String[] args) {
        int x =1,y=4;
        //int z = add(x,y);//接收返回值
       // System.out.println("×2="+z*2);

       // System.out.println("5的阶乘="+fac(5));
        int n = 5;
        func(n);
        System.out.println("实参仍为n="+n);
    }
}